﻿using System;
using System.Threading;
using System.Web.Mvc;
using KPIS.GERP.WEB.GFMS.Class.MemberShip;
using KPIS.GERP.WEB.GFMS.Class.MemberShip;
using WebMatrix.WebData;

namespace KPIS.GERP.WEB.GFMS.Class
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
    {
        private static SimpleMembershipInitializer _initializer;
        private static object _initializerLock = new object();
        private static bool _isInitialized;

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!WebSecurity.Initialized)
            {
                try
                {
                    // The line below will go to SimpleMembershipInitializer.cs
                    LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
                }
                catch (Exception exception)
                {
                    throw new Exception(exception.InnerException.Message, exception);
                }
            }
        }

    }
}